from flask import Flask,Markup
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import and_,or_
import os
import click


app = Flask(__name__)

#设置数据库URI
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///"+os.path.join(app.root_path,"data.db")
#配置变量决定是否追踪对象的修改
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

app.config['WTF_I18N_ENABLED'] = False
app.secret_key = "jfklsajf32423141234$@#$"

#限制请求报文最大长度,避免上传文件或其他大报文
app.config['MAX_CONTENT_LENGTH'] = 3 * 1024 * 1024

app.config["UPLOAD_PATH"] = os.path.join(app.root_path,"uploads")

#模板增加自定义全局对象
app.jinja_env.globals['author'] ={
    "name": "jack.w",
    "email":"jack@qq.com"
}
app.jinja_env.globals['jobs'] = ["Delphi开发","java开发","Python开发"]


def smiling(s):
    return s + " :)"

#添加自定义过滤器
app.jinja_env.filters["smiling"] =smiling

#自定义全局函数
@app.template_global(name='copyright')
def copyright():
    return "Copyright 2021 CYX科技 版权所有"


#自定义过滤器
@app.template_filter()
def musical(value):
    return value + Markup('&#9835')



from imdb import imdb_app
app.register_blueprint(imdb_app)



@app.cli.command()
def init_db():
    db.create_all()
    click.echo("数据库初始化")



@app.shell_context_processor
def make_shell_context():
    """
    注册Shell上下文处理器,通过字典方式返回变量，通过flask shell 命令启动后可以直接访问
    :return:
    """
    return dict(db=db)
if __name__ == '__main__':
    app.run(debug=True)